package com.ruoyi.course.service.impl;

import java.util.List;

import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.course.domain.SysPaper;
import com.ruoyi.course.mapper.SysPaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.SysPaperTopicMapper;
import com.ruoyi.course.domain.SysPaperTopic;
import com.ruoyi.course.service.ISysPaperTopicService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 试卷与题目关联Service业务层处理
 * 
 * @author yjf
 */
@Service
public class SysPaperTopicServiceImpl implements ISysPaperTopicService 
{
    @Autowired
    private SysPaperTopicMapper sysPaperTopicMapper;


    @Autowired
    private SysPaperMapper sysPaperMapper;

    /**
     * 查询试卷与题目关联
     *
     * @param sysPaperTopic 试卷与题目关联
     * @return 试卷与题目关联
     */
    @Override
    public SysPaperTopic getSysPaperTopicInfo(SysPaperTopic sysPaperTopic)
    {
        if(StringUtils.isNull(sysPaperTopic.getPaperId())||StringUtils.isNull(sysPaperTopic.getTopicId())){
            throw new ServiceException("请检查试卷参数、题目参数是否正确！");
        }
        return sysPaperTopicMapper.getSysPaperTopicInfo(sysPaperTopic);
    }

    /**
     * 统计题目
     *
     * @param sysPaperTopic 题目
     * @return 题目
     */
    @Override
    public int topicSum(SysPaperTopic sysPaperTopic)
    {
        sysPaperTopic.setDelFlag(SysConstants.CONSTANT_STR0);
        return sysPaperTopicMapper.topicSum(sysPaperTopic);
    }

    /**
     * 查询试卷与题目关联列表
     * 
     * @param sysPaperTopic 试卷与题目关联
     * @return 试卷与题目关联
     */
    @Override
    public List<SysPaperTopic> selectSysPaperTopicList(SysPaperTopic sysPaperTopic)
    {
        if(StringUtils.isNull(sysPaperTopic.getDelFlag())){
            sysPaperTopic.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        return sysPaperTopicMapper.selectSysPaperTopicList(sysPaperTopic);
    }

    /**
     * 新增试卷与题目关联
     * 
     * @param sysPaperTopic 试卷与题目关联
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysPaperTopic(SysPaperTopic sysPaperTopic)
    {

        SysPaperTopic info = sysPaperTopicMapper.getSysPaperTopicInfo(sysPaperTopic);

        if(StringUtils.isNotNull(info)){
           throw new ServiceException("试卷ID为"+sysPaperTopic.getPaperId()+",题目ID为"+sysPaperTopic.getTopicId()+"的组合已经存在！");
        }
        int result = sysPaperTopicMapper.insertSysPaperTopic(sysPaperTopic);

        //统计试题总数
        SysPaperTopic paperTopic = new SysPaperTopic();
        paperTopic.setPaperId(sysPaperTopic.getPaperId());   //试卷ID
        paperTopic.setTopicStatus(SysConstants.CONSTANT_INT0);    //题目状态
        paperTopic.setDelFlag(SysConstants.CONSTANT_STR0);
        int topicNum =  sysPaperTopicMapper.topicSum(paperTopic);

        //更新试卷的题目数量
        SysPaper sysPaper = new SysPaper();
        sysPaper.setPaperId(sysPaperTopic.getPaperId());
        sysPaper.setTopicNum(topicNum);
        sysPaper.setUpdateTime(DateUtils.getNowDate());
        sysPaperMapper.updateSysPaper(sysPaper);

        return result;
    }

    /**
     * 批量删除试卷与题目关联
     * 
     * @param sysPaperTopic 需要删除的试卷与题目关联主键
     * @return 结果
     */
    @Override
    public int deleteSysPaperTopic(SysPaperTopic sysPaperTopic)
    {
        return sysPaperTopicMapper.deleteSysPaperTopic(sysPaperTopic);
    }
}
